package com.bawei.module_home.ui.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_home.R
import com.bawei.module_home.databinding.HomeItemSimpleVideoBinding
import com.bawei.module_home.model.entity.SimpleVideoEntity
import kotlin.math.max

/**
 * @Author : yaotianxue
 * @Time : On 2023/6/25 16:32
 * @Description : SimpleVideoAdapter
 */
class SimpleVideoAdapter(private val list:MutableList<SimpleVideoEntity>):RecyclerView.Adapter<SimpleVideoAdapter.SimpleVideoViewHolder>() {
    class SimpleVideoViewHolder(val homeItemSimpleVideoBinding: HomeItemSimpleVideoBinding):RecyclerView.ViewHolder(homeItemSimpleVideoBinding.root)
    //创建SimpleVideoViewHolder
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SimpleVideoViewHolder {
        val simpleVideoBinding =
            DataBindingUtil.inflate<HomeItemSimpleVideoBinding>(LayoutInflater.from(parent.context),
                R.layout.home_item_simple_video,
                parent,
                false)
        return SimpleVideoViewHolder(simpleVideoBinding)
    }

    override fun onBindViewHolder(
        holder: SimpleVideoViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        super.onBindViewHolder(holder, position, payloads)
        //没有局部刷新
        if(payloads === null || payloads.size === 0){
            return
        }
        //关注的局部刷新，view隐藏
        if("focus".equals(payloads[0])){
            holder.homeItemSimpleVideoBinding.btnItemSimpleInfoSubscibe.visibility = View.GONE
        }

    }


    //绑定数据
    override fun onBindViewHolder(holder: SimpleVideoViewHolder, position: Int) {
        holder.homeItemSimpleVideoBinding.simpleVideoEntity = list[position]
        //关注
        holder.homeItemSimpleVideoBinding.btnItemSimpleInfoSubscibe.setOnClickListener {
            onItemChildClick?.invoke(it,position)
        }
        //点赞
        holder.homeItemSimpleVideoBinding.ivItemSimpleInfoAgree.setOnClickListener{
            onItemChildClick?.invoke(it,position)
        }
        //播放
        holder.homeItemSimpleVideoBinding.ivItemSimpleInfoPlay.setOnClickListener{
            onItemChildClick?.invoke(it,position)
        }
        //判断是否需要预加载：判断当前滑动的条目是否快到底了
        if(onPreload != null && position == max(list.size-1-preloadItemCount,0) && scrollState != RecyclerView.SCROLL_STATE_IDLE){
            onPreload?.invoke()//执行预加载函数
        }

    }
    //返回个数
    override fun getItemCount(): Int {
        return list.size
    }


    //预加载回调
    var onPreload:(()->Unit)?  = null
    //预加载偏移量，3 滑到倒数第3个预加载
    var preloadItemCount = 0
    //列表滚动状态 SCROLL_STATE_IDLE：停止滚动 其他：滚动状态
    var scrollState = RecyclerView.SCROLL_STATE_IDLE




    //事件监听：java 接口回调 kotlin lamda表达式代替接口回调
    //onItemClick是函数类型输入参数view: View,position:Int，输出参数Unit  无返回值
    //什么是高阶函数？？？参数为函数类型或者返回值为函数类型的函数为高阶函数
//      var onItemClick:(view: View,position:Int)->Unit
//      var onItemLongClick:(view: View,position:Int)->Boolean

      var onItemChildClick:((view: View,position:Int)->Unit)? = null
//      var onItemChildLongClick:(view: View,position:Int)->Boolean



    public fun getDataByPosition(position:Int):SimpleVideoEntity{
        return list.get(position)
    }


}